<?php require("sessionStart.php");


/*******************************************************************************************************************************************/
/*******************************************************************************************************************************************/
/*******************************************************************************************************************************************
 This is a complete example of the GetDriveSpace.php request link. The order is irrelevent 
 
 http://localhost/GetDriveSpace.php?startdate=2008-10-02&enddate=2008-10-03&drivespacedata=1&firstdate=1&lastdate=1&min=1&max=1&mean=1&quota

 
 
 
 Plus make sure that the ClientID is set in session variable. if you want to test this just set up the $_SESSION['ClientID']=josh2010 or alex2010.
 Plus you have to have the logfiles. as is. drivespace.log
 
 
 it returns JSON TYPE. the way it works first i construct an array of elements that is needed. then concert it to JSON format.
 
 


********************************************************************************************************************************************/
/*******************************************************************************************************************************************/
/*******************************************************************************************************************************************/

if( count($_REQUEST)> 0 )
{

	//setting a Default dates
	$lowerLimitDate = strtotime("1970-01-01");
	$higherLimitDate  = strtotime(date("Y-m-d"));
	
	//Boolean for checking the validity of date
	$dateBoolean = True;
	
	//for shoing Error message and Debugging
	$errMessage= "No Message";
	
	//.settig default values for min max and mean
	$min=999999;
	$max=-1;
	$mean=-12;
	
	//check to see if operation was a success
	$boolOperation = False;



		//call validateDate Function
		$dateBoolean = validateDate($lowerLimitDate ,$higherLimitDate, $errMessage);


	//Array that Holds the Needed Info
	$arrayDateValues=array();


	//for Debugging Purpose only
	$debugCount = 0;

	if ($dateBoolean)
	{
		//call the function to read from the file and save into arrays
		$boolOperation = readFileIntoArray($arrayDateValues,$debugCount,$lowerLimitDate,$higherLimitDate);

	}

	// checking to see if client has requested the min and if any data was collected from the log file
	if(isset($_REQUEST['min']) && count($arrayDateValues) != 0)
	{
		$min = getMin($arrayDateValues);
		$arrayDateValues['min'] = $min;

	}
		
	// checking to see if client has requested the max and if any data was collected from the log file
	if(isset($_REQUEST['max']) && count($arrayDateValues) != 0)
	{
		$max = getMax($arrayDateValues);
		$arrayDateValues['max'] = $max;
	}
	// checking to see if client has requested the mean and if any data was collected from the log file
	if(isset($_REQUEST['mean']) && count($arrayDateValues) != 0)
	{
		$mean = getMean($arrayDateValues);
		$arrayDateValues['mean'] = $mean;
	}
	// checking to see if client has requested the quota
	if(isset($_REQUEST['quota']))
	{
		require("GetClientQuota.php");
		$arrayDateValues['quota']=GetQuota($_SESSION['ClientID']."Quota");
	}

	//if date enetered by the user was good 
	if($boolOperation)
	{
		//reorder the array according to the quesry made
		reorderArray($arrayDateValues);
		//echo $debugCount;
		echo json_encode($arrayDateValues);


	}
	else
	{
		echo $errMessage;
	}
}



// Returns the Integer Day Value of the Year
function getIntDay($date)
{
	return (int)substr($date,8);
}

// Returns the Integer Month Value of the Year
function getIntMonth($date)
{
	return (int)substr($date,5,2);
}

// Returns the Integer Year Value of the Year
function getIntYear($date)
{
	return (int)substr($date,0,4);
}
//reorder array
function reorderArray(&$arrayDateValues)
{
		
		$tempLastDate = $arrayDateValues['lastdate'];
		$tempLastDriveSpace = $arrayDateValues['lastdrivespace'];
		unset($arrayDateValues['lastdrivespace']);
		unset($arrayDateValues['lastdate']);
		
	
	if(isset($_REQUEST['firstdate']))
	{
		$tempfirstDate = $arrayDateValues['firstdate'];
		unset($arrayDateValues['firstdate']);
		$arrayDateValues['firstdate'] = $tempfirstDate ;
		
	}
	if(isset($_REQUEST['lastdate']))
	{	
		$arrayDateValues['lastdate'] = $tempLastDate;
		
	}
	if(isset($_REQUEST['lastdrivespace']))
	{	
		$arrayDateValues['lastdrivespace'] = $tempLastDriveSpace ;
		
	}	
		
		
}



//Validates the Year ,iAguments are  Passed  by Reference
function validateDate(&$lowerLimitDate ,&$higherLimitDate,&$errMessage)
{
	// Boolean Variable to see
	$dateBoolean = True;
		if( isset($_REQUEST['startdate']) )
		{
			if (	checkdate(getIntMonth($_REQUEST['startdate']),getIntDay($_REQUEST['startdate']),getIntYear($_REQUEST['startdate']))		)
			{
				if(strtotime($_REQUEST['startdate']) >= $lowerLimitDate )
				{
					if ( $higherLimitDate >= strtotime($_REQUEST['startdate']) )
					{
						$dateBoolean = True;
						$lowerLimitDate = strtotime($_REQUEST['startdate']);
					}
					else
					{
						$dateBoolean = False;
						$errMessage ="Start Date is After End Date. Reverse the Order or correct them.";
					}
				}
				else
				{
					$dateBoolean = False;
					$errMessage =$errMessage . " + The date is can not be before 1970.";

				}
			}
			else
			{
				$dateBoolean = False;
				$errMessage =$errMessage . " + Date is not Valid.";
			}
			if(isset($_REQUEST['enddate']))
			{
				if( checkdate(getIntMonth($_REQUEST['enddate'])  ,getIntDay($_REQUEST['enddate'])  ,getIntYear($_REQUEST['enddate']))&&  strtotime($_REQUEST['enddate']) <= $higherLimitDate )
				{
					$dateBoolean = True;
					$higherLimitDate = strtotime($_REQUEST['enddate']);
				}
				else
				{
					$dateBoolean = False;
					$errMessage =$errMessage. " + problem with end date Date format";
				}
			}
		}
		return $dateBoolean;
}


//Reads The Result into an Array
function readFileIntoArray(&$arrayDateValues,&$debugCount,$lowerLimitDate,$higherLimitDate)
{
	$firstdate = False;
	$lastdate = False;
	$lastdrivespace = False;
	$findLowerLimit = True;



			//set the flag for the firstdate
			if(isset($_REQUEST['firstdate']))
				$firstdate = True;
			//set the flag for the lastdate
			if(isset($_REQUEST['lastdate']))
				$lastdate = True;
			//set the flag for the lastdrivespace
			if(isset($_REQUEST['lastdrivespace']))
				$lastdrivespace = True;

			$file=fopen( setFilePath(),"r") or exit("Unable to open file!");


			while(!feof($file))
			  {

			  		$tempLine = trim(fgets($file));

			  		//check to see if it is not a comment or blank line
			  		if(substr($tempLine,0,1) != "#" && strlen($tempLine)>0)
			  		{
						$token = preg_split('/\s+/', $tempLine);



						if($firstdate)
						{
							$arrayDateValues['firstdate'] = $token[0];
							$firstdate = False;
						}

						if ($findLowerLimit && strtotime($token[0]) >= $lowerLimitDate)
						{
						$arrayDateValues[$token[0]] = $token[1];
						$debugCount++;
						$findLowerLimit = False;

						}

						elseif (!$findLowerLimit && strtotime($token[0]) < $higherLimitDate)
						{
							$arrayDateValues[$token[0]] = $token[1];
							$arrayDateValues['lastdate'] = $token[0];
							$arrayDateValues['lastdrivespace'] = $token[1];
							$debugCount++;

						}

						elseif (strtotime($token[0]) == $higherLimitDate)
						{
							$arrayDateValues[$token[0]] = $token[1];
							$debugCount++;
							if($lastdate || $lastdrivespace)
							{
								while(!feof($file))
								{
									$tempLine = trim(fgets($file));

									//check to see if it is not a comment or blank line
									if(substr($tempLine,0,1) != "#" && strlen($tempLine)>0)
									{
										$token = preg_split('/\s+/', $tempLine);
									}

								}
								$arrayDateValues['lastdate'] = $token[0];
								$arrayDateValues['lastdrivespace'] = $token[1];
							}
							else
								break;


						}//elseif

					}//if not a comment

			  }//while loop

	  fclose($file);
	  
	  
	  if(count($arrayDateValues) > 0)
	  {
		return True;
	  }

}


function setFilePath()
{
	return "../Clients/".$_SESSION['ClientID']."/drivespace.log";
}


//Getting the MInimum in the array
function getMin($arrayDateValues)
{
	// default value
	$min =888888888;
	foreach ($arrayDateValues as $item)
	{

		$item = (int) substr($item,0,strlen($item)-1);

		if($item<$min)
		{
			$min = $item;
		}
	}
	return $min;
}
function getMax($arrayDateValues)
{
	$max =-1;
	foreach ($arrayDateValues as $item)
	{

		$item = (int) substr($item,0,strlen($item)-1);

		if( $item > $max )
		{
			$max = $item;
		}
	}
	return $max;
}
function getMean($arrayDateValues)
{
	$mean = 0 ;
	foreach ($arrayDateValues as $item)
	{
		$mean = $mean + (int) substr($item,0,strlen($item)-1);
	}
	return $mean / count($arrayDateValues);
}
?>
